package com.at.clinica.repository.jpa;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.at.clinica.repository.IProjectRepository;

@Repository
public abstract class JpaProjectRepository<EntityType> implements IProjectRepository<EntityType> {

	@PersistenceContext
	protected EntityManager em;
	
	@Override
	public void save(EntityType entity) throws Exception { 
			em.persist(entity);
	}

	@Override
	public EntityType findById(Long id, Class clase) throws Exception {
		EntityType entity;
		entity= (EntityType) em.find(clase, id);
		return entity;
	}

	@Override
	public void delete(EntityType entity) throws Exception {
		em.remove(entity);
	}

	@Override
	public List<EntityType> getListEntity(Class clase) throws Exception {
		Query query = null;
		query = em.createQuery("FROM " + clase.getSimpleName());
		return query.getResultList();
	}

}
